package com.fr.data.core.db.dialect;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.data.core.db.TableProcedure;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OracleDialect extends AbstractDialect
{
  private int oracleFetchSize = 50;
  private String sequenceName = "";
  private String table = "";
  private String column = "";

  public String getSequenceName()
  {
    return this.sequenceName;
  }

  public void setSequenceName(String paramString)
  {
    this.sequenceName = paramString;
  }

  public OracleDialect()
  {
    this.left_quote = "\"";
    this.right_quote = "\"";
  }

  public int getFetchSize()
  {
    return this.oracleFetchSize;
  }

  public void setFetchSize(int paramInt)
  {
    this.oracleFetchSize = paramInt;
  }

  public String createSequence(Connection paramConnection, String paramString1, String paramString2, String paramString3)
  {
    try
    {
      createOracleSequence(paramConnection, paramString1, paramString2);
    }
    catch (SQLException localSQLException)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException.getMessage(), localSQLException);
    }
    return paramString3;
  }

  private void createOracleSequence(Connection paramConnection, String paramString1, String paramString2)
    throws SQLException
  {
    String str = paramString1.substring(1, paramString1.length() - 1) + "_sequence".toUpperCase();
    this.sequenceName = str;
    this.table = paramString1;
    this.column = paramString2;
    Statement localStatement = paramConnection.createStatement();
    try
    {
      localStatement.execute("drop sequence " + str.toUpperCase());
      localStatement.close();
    }
    catch (Exception localException)
    {
      localStatement.close();
    }
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement("create sequence " + str + " start with 1 increment by 1 minvalue 0 maxvalue 999999999999 cycle nocache");
    localPreparedStatement.execute();
    localPreparedStatement.close();
  }

  public static String getOracleIncNextValue(String paramString)
  {
    return paramString + "_SEQUENCE.nextval";
  }

  public static String getOravleIncCURRValue(String paramString)
  {
    if ((paramString.startsWith("\"")) && (paramString.endsWith("\"")))
      paramString = paramString.substring(1, paramString.length() - 1);
    return paramString + "_SEQUENCE.CURRVAL";
  }

  public String getIdentitySelectString(String paramString1, String paramString2, int paramInt)
    throws Exception
  {
    return "select " + getOravleIncCURRValue(paramString1) + " from dual";
  }

  public void createTrigger(Connection paramConnection)
  {
    if (StringUtils.isBlank(this.sequenceName))
      return;
    String str1 = this.table.substring(1, this.table.length() - 1) + "Trigger";
    StringBuffer localStringBuffer = new StringBuffer();
    String str2 = this.column;
    if (this.column.matches("^\".*\"$"))
      str2 = this.column.substring(1, this.column.length() - 1);
    localStringBuffer.append("create or replace trigger ").append(str1).append(" before insert on ").append(this.table).append(" for each row begin select ").append(this.sequenceName).append(".nextval into :new.").append(str2).append(" from dual; end;");
    Statement localStatement = null;
    try
    {
      localStatement = paramConnection.createStatement();
      localStatement.execute(localStringBuffer.toString());
      localStatement.close();
    }
    catch (SQLException localSQLException1)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException1.getMessage(), localSQLException1);
      try
      {
        localStatement.close();
      }
      catch (SQLException localSQLException2)
      {
        FRContext.getLogger().log(Level.WARNING, localSQLException2.getMessage(), localSQLException2);
      }
    }
  }

  public String columnType2SQL(int paramInt, String paramString)
  {
    switch (paramInt)
    {
    case 16:
      return "NUMBER(1,0)";
    case -6:
    case -5:
    case 4:
    case 5:
      return "NUMBER(" + paramString + ")";
    case 2:
    case 3:
    case 6:
    case 7:
    case 8:
      return "NUMBER(" + paramString + ")";
    case -3:
    case -2:
      return "RAW(" + paramString + ")";
    case -4:
      return "LONG RAW";
    case -1:
      return "LONG";
    case 12:
      return "VARCHAR2(" + paramString + ")";
    case 1:
      return "CHAR(" + paramString + ")";
    case 1111:
      return "NCLOB";
    case -7:
      return "VARCHAR2(1)";
    }
    return TypeUtils.getTypeName(paramInt);
  }

  private String getOracleSQL(String paramString, boolean paramBoolean)
  {
    String str = "";
    if (!(paramBoolean))
    {
      str = "select owner,table_name from all_tables where tablespace_name<>'SYSTEM' and tablespace_name<>'SYSAUX'";
      if (!(StringUtils.isBlank(paramString)))
        str = str + " and owner='" + paramString + "'";
    }
    else
    {
      str = "select owner,table_name from all_tables";
      if (!(StringUtils.isBlank(paramString)))
        str = str + " where owner='" + paramString + "'";
    }
    return str;
  }

  public List getTableProcedure(Connection paramConnection, String paramString, boolean paramBoolean)
    throws Exception
  {
    ArrayList localArrayList = new ArrayList();
    String str = getOracleSQL(paramString, paramBoolean);
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement(str);
    ResultSet localResultSet = localPreparedStatement.executeQuery();
    while (localResultSet.next())
      localArrayList.add(new TableProcedure(localResultSet.getString("owner"), localResultSet.getString("table_name"), "TABLE"));
    localResultSet.close();
    localPreparedStatement.close();
    return localArrayList;
  }

  public boolean supportsLimit()
  {
    return true;
  }

  public String getLimitString(String paramString, int paramInt1, int paramInt2)
  {
    paramString = paramString.trim();
    int i = 0;
    if (paramString.toLowerCase().endsWith(" for update"))
    {
      paramString = paramString.substring(0, paramString.length() - 11);
      i = 1;
    }
    StringBuffer localStringBuffer = new StringBuffer(paramString.length() + 100);
    localStringBuffer.append("select * from ( select row_.*, rownum rownum_ from ( ");
    localStringBuffer.append(paramString);
    localStringBuffer.append(" ) row_ where rownum <= " + (paramInt1 + paramInt2) + ") where rownum_ > " + paramInt1);
    if (i != 0)
      localStringBuffer.append(" for update");
    return localStringBuffer.toString();
  }

  public String quartzDelegateClass()
  {
    return "com.fr.third.org.quartz.impl.jdbcjobstore.oracle.OracleDelegate";
  }
}